#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   loadClass.py
@Time    :   2024/01/16 16:10:51
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   加载爬虫类
'''

import inspect
import os
import sys
from init.maker import Maker
sys.path.append("..")
from instance import *

# 扫描路径获取模块
def import_classes(path):
    classNameList = []
    for dir in os.listdir(path):
        if os.path.isdir(os.path.join(path, dir)):
            if dir == "__pycache__":
                continue
            classNameList.append(dir)
    print(classNameList)
    return classNameList

# 加载模块，将所需的类以字典的方式传入列表，并返回
def get_classes(classNameList):
    classes = {}
    for className in classNameList:
        clsmembers = inspect.getmembers(eval(className), inspect.isclass)
        class_list = []
        for (name, _) in clsmembers:
            exec(f"from instance.{className} import {name}", globals())
            print(eval(name))
            class_list.append(eval(name))
        classes[className] = class_list

    return classes

if __name__ == "__main__":
    classNameList = import_classes("../")
    classes = get_classes(classNameList)
    print(classes)
    username = "######"
    password = "######"
    key = "普工"
    pages = 1
    path = "./"
    db_name = "pugong.db"
    table_name = "leqing"
    dataPath = path + db_name
    Maker(classes["LeQing"], dataPath, username, password, key, 1, pages, path, table_name)
